package com.txzt.gaea.business.util;

import com.txzt.base.core.RtnInfo;
import com.txzt.base.util.SpringContextHolder;
import com.txzt.gaea.api.client.AdminUserRemoteService;
import com.txzt.gaea.base.model.DictDetail;
import com.txzt.gaea.base.model.DictInfo;
import com.txzt.gaea.base.service.DictInfoService;
import com.txzt.gaea.business.project.controller.ProjectFundController;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.dto.UserSingleDetailQueryDTO;
import com.txzt.gaea.flow.vo.FlowBizStepVo;
import com.txzt.gaea.provide.service.biz.DeptApiService;
import com.txzt.gaea.vo.AdminUserVO;
import com.txzt.gaea.vo.UserDetailVO;
import com.txzt.gaea.vo.ZTreeNodeVO;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TranslateUtil {

    private static final Logger logger = LoggerFactory.getLogger(TranslateUtil.class);

    private static final DeptApiService deptApiService;
    private static final AdminUserRemoteService adminUserRemoteService;
    private static final List<ZTreeNodeVO> deptList;
    private static final SQLManager SQLManager;

    static {
        deptApiService = SpringContextHolder.getApplicationContext().getBean(DeptApiService.class);
        adminUserRemoteService = SpringContextHolder.getApplicationContext().getBean(AdminUserRemoteService.class);
        deptList = deptApiService.deptInfoList();
        SQLManager = SpringContextHolder.getApplicationContext().getBean(SQLManager.class);
    }

    public static String getDepartmentName(Long id){
        if (null != id){
            for (ZTreeNodeVO z : deptList) {
                if (Long.parseLong(z.getId()) == id){
                    return z.getName();
                }
            }
        }
        return "";
    }

    public static Long getDepartmentId(String name){
        if (StringUtils.isNotEmpty(name)){
            for (ZTreeNodeVO z : deptList) {
                if (z.getName().equals(name)){
                    return Long.valueOf(z.getId());
                }
            }
        }
        return null;
    }

    public static String getUserName(Long id){
        if (null != id) {
            UserSingleDetailQueryDTO userSingleDetailQueryDTO = new UserSingleDetailQueryDTO();
            userSingleDetailQueryDTO.setUserId(id);
            RtnInfo<UserDetailVO> result = adminUserRemoteService.singleDetailQuery(userSingleDetailQueryDTO);
            if (null == result) {
                logger.info("logger:  adminUserRemoteService --> singleDetailQuery [Remote invoke exception !]  query:user id -->" + id);
                return "";
            }
            UserDetailVO user = result.getData();
            if (null == user) {
                logger.info("logger:  adminUserRemoteService --> singleDetailQuery [user not found !]  query:user id -->" + id);
                return "";
            }
            return user.getName();
        }
        return "";
    }

    public static Boolean getCheck(List<FlowBizStepVo> flowStep){
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        for (FlowBizStepVo flowBizStepVo : flowStep){
            if (null == flowBizStepVo.getStepStatus()){
                List<String> list = Arrays.stream(adminUserVO.getRoleid().split(",")).map(String::valueOf).collect(Collectors.toList());
                return list.contains(flowBizStepVo.getStepMember());
            }
        }
        return false;
    }

    /**
     * @author cong.fang
     * @since  2022-09-09
     *
     * 用户ids翻译用户名称
     *
     * @param userIds
     * @return 用户名称（逗号分隔字符串）
     */
    public static String getUserNames(String userIds) {
        if (StringUtils.isEmpty(userIds)) {
            return "";
        }
        List<Long> userIdList = Arrays.stream(userIds.split(",")).map(Long::valueOf).collect(Collectors.toList());
        List<String> userNameList = new ArrayList<>();
        for (Long id :userIdList){
            userNameList.add(TranslateUtil.getUserName(id));
        }
        return StringUtils.join(userNameList, ",");
    }

    public static String getPlanType(String planType,String subTypeName){
        if (StringUtils.isEmpty(subTypeName)) {
            return "";
        }
        DictDetail dictDetail = SQLManager.query(DictDetail.class)
                .andEq("value",planType)
                .andEq("is_delete", IsDeleteEnum.NOTDELETE.getCode())
                .single();
        if (null == dictDetail) {
            return "";
        }
        DictDetail result = SQLManager.query(DictDetail.class)
                .andEq("dict_id",dictDetail.getSubDictId())
                .andEq("label",subTypeName)
                .andEq("is_delete", IsDeleteEnum.NOTDELETE.getCode())
                .single();
        return null == result ? "" : result.getValue();
    }


    public static String getPlanTypeValue(String planType,String dictCode){
        DictInfo dictInfo = SQLManager.query(DictInfo.class)
                .andEq("dict_code",dictCode)
                .andEq("is_delete", IsDeleteEnum.NOTDELETE.getCode())
                .single();
        DictDetail result = SQLManager.query(DictDetail.class)
                .andEq("dict_id",dictInfo.getId())
                .andEq("label",planType)
                .andEq("is_delete", IsDeleteEnum.NOTDELETE.getCode())
                .single();
        return null == result ? "" : result.getValue();
    }
}
